class unionFind:
    def __init__(self,province):#初始化每个节点的父节点都是自己
        row = len(province)
        self.root = [-1] * row
        for i in range(len(self.root)):
            self.root[i] = i
    def union(self,x,y):
        rootX = self.find(self.root[x])
        rootY = self.find(self.root[y])
        if rootX != rootY:
            self.root[rootY] = rootX
    def find(self,x):
        if self.root[x] == x:
            return self.root[x]
        else:
            self.root[x] = self.find(self.root[x])
            return self.root[x]
isConnected = [[1,0,0,1],[0,1,1,0],[0,1,1,1],[1,0,1,1]]
if __name__ == '__main__':
    provinceUF = unionFind(isConnected)
    for i in range(len(isConnected)):
        for j in range(i):
            if isConnected[i][j] == 1:
                print("{},{}".format(i,j))
                provinceUF.union(i,j)
                print(provinceUF.root)
    for i in range(len(isConnected)):
        provinceUF.find(i)
    proCount = [0] * len(isConnected)
    for i in range(len(provinceUF.root)):
        proCount[provinceUF.root[i]] = 1
    count = 0
    for i in proCount:
        if i == 1:
            count += 1
    print(count)
    print(provinceUF.root)